import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../public/publicMethods.dart';
import 'dart:convert';

class Daily extends StatelessWidget {
  const Daily({super.key});

  @override
  Widget build(BuildContext context) {
    return _Daily();
  }
}

class _Daily extends StatefulWidget {
  @override
  _DailyState createState() => new _DailyState();
}

/// RestorationProperty objects can be used because of RestorationMixin.
class _DailyState extends State<_Daily>{
  double rem = 0;
  Map list = {"data":[]};

  getDaily() async{
    Map response = await ajax.postToken(path:'/competition-app/public/api/users/getDaily');
    if(response['statusCode'] == 200){
      Map<String, dynamic> userMap = jsonDecode(response['body']);

      dailyList = userMap;
      if(userMap['code'] == 1){
        setState(() {
          list = userMap;

        });
      }
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDaily();
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(statusBarColor: Colors.transparent,statusBarIconBrightness: Brightness.dark));
  }

  double getStatusBarHeight() {
    return MediaQuery.of(context).padding.top;
  }

  @override
  Widget build(BuildContext context) {
    rem =  MediaQuery.of(context).size.width/1080;
    return Scaffold(
      body: Container(
        alignment: Alignment.topCenter,
        width: 9999,
        height: MediaQuery.of(context).size.height,
        color: Color.fromRGBO(242, 241, 246, 1),
        child:

        Container(
          width: 990*rem,
          height:MediaQuery.of(context).size.height,
          margin: EdgeInsets.fromLTRB(0, getStatusBarHeight(), 0, 0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  width: 990*rem,
                  padding: EdgeInsets.all(30*rem),
                  child: Hero(tag: 'title', child: Text('日报',style: TextStyle(color:Colors.black,decoration: TextDecoration.none,fontWeight: FontWeight.normal, fontSize: 100*rem),),
                ),),

                Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    //border: Border.all(color: Colors.black87,width: 1.0,style: BorderStyle.solid),
                    borderRadius: BorderRadius.circular(30*rem),


                  ),
                  padding: EdgeInsets.fromLTRB(0*rem,30*rem, 0*rem,0),
                  height:MediaQuery.of(context).size.height- 500*rem,
                  child: ListView.builder(
                      itemCount: list["data"].length,
                      itemBuilder: (context, index) {
                        return Container(
                          padding: EdgeInsets.fromLTRB(80*rem,0*rem, 80*rem,0),
                          alignment: Alignment.topLeft,
                          color: Colors.white,
                          child: Stack(
                            alignment: Alignment.center,
                            children: [
                            Container(
                              height: 150*rem,
                              child:  Column(
                                mainAxisAlignment: MainAxisAlignment.spaceAround,
                                children: [
                                  Container(
                                    width:9999,
                                    child: Text(list["data"][index]['content'].substring(0, 5),textAlign:TextAlign.left,style: TextStyle(fontSize: 40*rem,fontWeight: FontWeight.bold),),
                                  ),
                                  Container(
                                    width:9999,
                                    child: Text(list["data"][index]['created_at'].toString(),textAlign:TextAlign.left,style: TextStyle(fontSize: 40*rem,color: Color.fromRGBO(135 , 135, 135, 1)),),
                                  ),

                                  Container(
                                    width:9999,
                                    color:Color.fromRGBO(135 , 135, 135, 0.4) ,
                                    height: 1,
                                  ),
                                ],
                              )),
                              Container(
                                width:9999,
                                height:140*rem,
                                child: TextButton (
                                    style: ButtonStyle(
                                      backgroundColor: MaterialStateProperty.all(Color.fromRGBO(0, 0, 0, 0)),
                                      elevation: MaterialStateProperty.all(20),
                                      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                                        RoundedRectangleBorder(
                                          borderRadius: BorderRadius.zero,
                                        ),
                                      ),
                                      foregroundColor:
                                      MaterialStateProperty.all(Colors.white),
                                    ),
                                    onPressed:(){

                                      Navigator.push(
                                        context,
                                        PageRouteBuilder(
                                          transitionDuration: Duration(milliseconds: 300),
                                          pageBuilder: (context, animation, secondaryAnimation) => DailyDetailed(indexs:index),
                                          transitionsBuilder: (context, animation, secondaryAnimation, child) {
                                            // 自定义动画
                                            return SlideTransition(
                                              position: Tween<Offset>(
                                                begin: const Offset(1.0, 0.0),
                                                end: const Offset(0.0, 0.0),
                                              ).animate(animation),
                                              child: child,
                                            );
                                          },
                                        ),
                                      );

                                    },
                                    child: Text('')
                                ),
                              )

                            ],
                          )
                        );
                      }),
                ),

                Container(
                  padding: EdgeInsets.all(30*rem),
                  child: Container(
                    height: 127*rem,
                    child: ElevatedButton(
                      style:ButtonStyle(
                          backgroundColor:MaterialStateProperty.all(Color.fromRGBO(11, 135, 255, 1)),
                          foregroundColor: MaterialStateProperty.all(Colors.white),
                          shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(8*rem))),
                          side:MaterialStateProperty.all(BorderSide(
                              width:3*rem,color:Color.fromRGBO(11, 135, 255, 1)
                          ))
                      ),
                      child: Text('填写日报',
                          style:TextStyle(
                            fontSize:40*rem,
                          )
                      ),
                      onPressed: () {


                        Navigator.push(
                          context,
                          PageRouteBuilder(
                            transitionDuration: Duration(milliseconds: 300),
                            pageBuilder: (context, animation, secondaryAnimation) => DailyDetailed(indexs:-1),
                            transitionsBuilder: (context, animation, secondaryAnimation, child) {
                              // 自定义动画
                              return SlideTransition(
                                position: Tween<Offset>(
                                  begin: const Offset(1.0, 0.0),
                                  end: const Offset(0.0, 0.0),
                                ).animate(animation),
                                child: child,
                              );
                            },
                          ),
                        );

                      },
                    ),
                  ),
                )
              ]
          ),
        )


      ),
    );
  }
}


class DailyDetailed extends StatelessWidget {
  int indexs;
  DailyDetailed({super.key,required this.indexs});

  @override
  Widget build(BuildContext context) {
    return _DailyDetailed(indexs:indexs);
  }
}

class _DailyDetailed extends StatefulWidget {
  int indexs;
  _DailyDetailed({required this.indexs});

  @override
  _DailyDetailedState createState() => new _DailyDetailedState();
}


/// RestorationProperty objects can be used because of RestorationMixin.
class _DailyDetailedState extends State<_DailyDetailed>{
  double getStatusBarHeight() {
    return MediaQuery.of(context).padding.top;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  double rem = 0;
  @override
  Widget build(BuildContext context) {

    rem =  MediaQuery.of(context).size.width/1080;
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child:
        Container(
          margin: EdgeInsets.fromLTRB(0, getStatusBarHeight(), 0, 0),
          width: 990*rem,
          child:Column(
            children: [

              Container(
                width: 990*rem,
                height: 150*rem,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                     Container(
                      child: Hero(tag: 'title', child:Text('日报',style: TextStyle(color:Color.fromRGBO(212, 170, 84, 1),decoration: TextDecoration.none,fontWeight: FontWeight.normal, fontSize: 55*rem),),
                      ),),


                    Visibility(
                      visible: widget.indexs<0,
                      child: Container(
                        child: TextButton(
                          child: Text("完成", style: TextStyle(color:Color.fromRGBO(212, 170, 84, 1),fontSize: 55*rem)),
                          onPressed: () {},
                        ),
                    ))


                  ],
                ),
              ),

            Visibility(
            visible: widget.indexs<0,
              child:
              Container(
                child: TextField(
                  maxLines: null, // 设置为null允许无限行
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    hintText: '请填写今天的日报...',
                  ),
                ),
              )),

              Visibility(
                  visible: widget.indexs>-1,
                  child:
                  Container(
                    child: Text(dailyList['data'][widget.indexs<0?0:widget.indexs]['content'],style: TextStyle(fontSize: 60*rem),)
                  )),

            ],
          ),
        )
      ),
    );
  }
}
